/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* MpmProjectResourcePoolService
* 部门审批人资源池
* 
* 修改纪录
* 2020-12-25 版本：1.0 lanjianwei 创建。
* @version 版本：1.0
* @author 作者：lanjianwei
* 创建日期：2020-12-25
*/
package com.topscomm.mpm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.exception.MessageException;
import com.topscomm.basic.exception.ServiceException;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectResourcePoolService;
import com.topscomm.mpm.service.impl.auto.MpmProjectResourcePoolServiceAuto;

@Service(value = "mpmProjectResourcePoolService")
@Scope(value = "singleton")
public class MpmProjectResourcePoolService extends MpmProjectResourcePoolServiceAuto implements IMpmProjectResourcePoolService {

    @Override
    protected void beforeInsertBatch(List<BasicEntity> listEntity) throws ServiceException {
        for(BasicEntity entity:listEntity){
            String levelList = ConvertUtil.convertToString(entity.getValue("projectlevel"));
            String[] split = levelList.split(",");
            //查询此项目类型、此级别、此可审批的部门、此人是否存在于数据库
            StringBufferProxy whereSql = new StringBufferProxy();
            whereSql.appendSingle("1=1");
            whereSql.appendSingle("and projecttype = '{0}'",ConvertUtil.convertToString(entity.getValue("projecttype")));
            whereSql.appendSingle("and deptname like '%{0}%'",ConvertUtil.convertToString(entity.getValue("deptname")));
            whereSql.appendSingle("and usercode  = '{0}'",ConvertUtil.convertToString(entity.getValue("usercode")));
            if(split.length==1){
                whereSql.appendSingle("and (projectlevel like '%{0}%')",split[0]);
            }else{
                for (int i = 0; i < split.length; i++) {
                    if (i == 0) {
                        whereSql.appendSingle("and ( projectlevel like '%{0}%' or ", split[i]);
                    } else if (i == split.length - 1) {
                        whereSql.appendSingle(" projectlevel like '%{0}%' )", split[i]);
                    } else {
                        whereSql.appendSingle(" projectlevel like '%{0}%' or ", split[i]);
                    }
                }
            }
            //查询数量
            List<Map<String, Object>> list = this.queryMapByWhere(whereSql.toString());
            if(list.size()>0){
                Map<String, Object> objectMap = list.get(0);
                StringBufferProxy message = new StringBufferProxy();
                message.appendSingle("本条数据和【项目类型：{0}；项目等级：{1}；决策部门：{2}；审批人：{3}】有重复，请检查。",ConvertUtil.convertToString(objectMap.get("projecttype")),
                    ConvertUtil.convertToString(objectMap.get("dispprojectlevel")),
                    ConvertUtil.convertToString(objectMap.get("deptname")),
                    ConvertUtil.convertToString(objectMap.get("username")));
                throw new MessageException(message.toString());
            }
        }
    }

    @Override
    protected void updateBefore(Map<String, Object> map) throws ServiceException {
        //查询更新后的数据是否和数据库中的数据重复
        String[] split = ConvertUtil.convertToString(map.get("projectlevel")).split(",");
        ArrayList<String> levelList = new ArrayList<>(Arrays.asList(split));

        //查询此项目类型、此级别、此可审批的部门、此人是否存在于数据库
        StringBufferProxy whereSql = new StringBufferProxy();
        whereSql.appendSingle("1=1");
        whereSql.appendSingle("and projecttype = '{0}'",ConvertUtil.convertToString(map.get("projecttype")));
        whereSql.appendSingle("and deptname like '%{0}%'",ConvertUtil.convertToString(map.get("deptname")));
        whereSql.appendSingle("and usercode  = '{0}'",ConvertUtil.convertToString(map.get("usercode")));
        whereSql.appendSingle("and id  != '{0}'",ConvertUtil.convertToString(map.get("id")));

        if(levelList.size()==1){
            whereSql.appendLineSingle("and (projectlevel like '%{0}%')",split[0]);
        }else{
            for (int i = 0; i < split.length; i++) {
                if (i == 0) {
                    whereSql.appendSingle("and ( projectlevel like '%{0}%' or ", split[i]);
                } else if (i == split.length - 1) {
                    whereSql.appendSingle(" projectlevel like '%{0}%' )", split[i]);
                } else {
                    whereSql.appendSingle(" projectlevel like '%{0}%' or ", split[i]);
                }
            }
        }
        //查询数量
        List<Map<String, Object>> list = this.queryMapByWhere(whereSql.toString());
        if(list.size()>0){
            Map<String, Object> objectMap = list.get(0);
            StringBufferProxy message = new StringBufferProxy();
            message.appendSingle("本条数据和【项目类型：{0}；项目等级：{1}；决策部门：{2}；审批人：{3}】有重复，请检查。",ConvertUtil.convertToString(objectMap.get("projecttype")),
                ConvertUtil.convertToString(objectMap.get("dispprojectlevel")),
                ConvertUtil.convertToString(objectMap.get("deptname")),
                ConvertUtil.convertToString(objectMap.get("username")));
            throw new MessageException(message.toString());
        }
    }
}
